﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuanLyTiemThuocTay.cls;

namespace QuanLyTiemThuocTay.Report
{
    public partial class rptDoanhThu : DevExpress.XtraReports.UI.XtraReport
    {
        QLTTDataContext db = new QLTTDataContext();
        List<HDXUAT> hdx;
        List<CHITIETXUAT> lctx = new List<CHITIETXUAT>();
        private int STT;
        int tongtien = 0;
        private DateTime den;
        private DateTime tu;
        public rptDoanhThu()
        {
            InitializeComponent();
            prTu.Value = prDen.Value = DateTime.Now;
            STT = 0;
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (hdx.Count>0)
            {
                STT++;
                xrTT.Text = STT.ToString();
                try { xrSoPhieu.Text = "  " + hdx[STT - 1].SoHDXuat; }
                catch { }
                try { xrNgayLap.Text = "  " + Convert.ToDateTime(hdx[STT - 1].NgayXuat.Value).Date; }
                catch { }
                bool hdbanle = hdx[STT - 1].BanLe;
                try
                {
                    if (hdbanle)
                        xrLoai.Text = "HĐ bán lẻ";
                    else
                        xrLoai.Text = "HĐ bán buôn";
                }
                catch { }
                int tongtiencua1hoadon = 0;
                int dongiacua1loaithuoc = 0;
                try
                {
                    lctx = db.CHITIETXUATs.Where(x => x.SoHDXuat == hdx[STT - 1].SoHDXuat).ToList();
                    foreach (var item in lctx)
                    {
                        if (hdbanle)
                            dongiacua1loaithuoc = db.GIATHUOCs.SingleOrDefault(x => x.MaDonGia == item.MaDonGia).GiaBanLe;
                        else
                            dongiacua1loaithuoc = db.GIATHUOCs.SingleOrDefault(x => x.MaDonGia == item.MaDonGia).GiaBanBuon;
                        tongtiencua1hoadon += dongiacua1loaithuoc * (int)item.SoLuong;
                    }
                    xrThanhTien.Text = tongtiencua1hoadon + "";
                    tongtien += tongtiencua1hoadon;
                }
                catch { }
                xrTongTien.Text = tongtien + " vnđ";
                xrSoTienChu.Text = "Tổng cộng (bằng chữ): " + clsHam.DocTienBangChu(tongtien, " đồng y.");
            }
        }

        private void rptDoanhThu_ParametersRequestSubmit(object sender, DevExpress.XtraReports.Parameters.ParametersRequestEventArgs e)
        {
            tu = Convert.ToDateTime(prTu.Value);
            den = Convert.ToDateTime(prDen.Value);
            if (tu.Date == den.Date)
                xrNgay.Text = "Ngày " + tu.Day + "/" + tu.Month + "/" + tu.Year ;
            else
                xrNgay.Text = ("Từ ngày " + tu.Day + "/" + tu.Month + "/" + tu.Year + " đến ngày " + den.Day + "/" + den.Month + "/" + den.Year).ToUpper();

           hdx = db.HDXUATs.Where(x => Convert.ToDateTime(x.NgayXuat.Value).Date >= tu.Date && Convert.ToDateTime(x.NgayXuat.Value).Date <= den.Date).ToList();
           try
           {
               hdx.OrderBy(x => x.NgayXuat);
               DataSource = hdx;
           }
           catch { }
        }

    }
}
